<?php

namespace iBrand\Component\Goods\Models;

use iBrand\Component\Discount\Contracts\DiscountItemContract;
use iBrand\Component\Category\Category;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

class Goods extends Model implements Transformable, DiscountItemContract
{
    use SoftDeletes, TransformableTrait;

    protected $guarded = ['id'];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $prefix = config('ibrand.app.database.prefix', 'ibrand_');

        $this->setTable($prefix . 'base_goods');
    }

    public function brand()
    {
        return $this->belongsTo(Brand::class, 'brand_id');
    }

    public function supplier()
    {
        return $this->belongsTo(Supplier::class, 'supplier_id');
    }

    public function goodsModel()
    {
        return $this->belongsTo(GoodsModel::class, 'model_id', 'id');
    }

    public function product()
    {
        return $this->hasMany(GoodsProduct::class, 'goods_id');
    }

    public function attributes()
    {
        return $this->belongsToMany('iBrand\Component\Goods\Models\Attribute', config('ibrand.app.database.prefix', 'ibrand_') . 'base_goods_attribute_relation', 'goods_id', 'attribute_id')->withPivot('attribute_value_id', 'model_id', 'attribute_value');
    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

    public function categories()
    {
        return $this->belongsToMany('iBrand\Component\Category\Category', config('ibrand.app.database.prefix', 'ibrand_') . 'base_goods_category', 'goods_id', 'category_id');
    }

    public function hasManySpec()
    {
        return $this->belongsToMany('iBrand\Component\Goods\Models\Spec', config('ibrand.app.database.prefix', 'ibrand_') . 'base_goods_spec_relation', 'goods_id', 'spec_id');
    }

    public function specs()
    {
        return $this->belongsToMany('iBrand\Component\Goods\Models\Spec', config('ibrand.app.database.prefix', 'ibrand_') . 'base_goods_spec_relation', 'goods_id', 'spec_id')
            ->withPivot('spec_value', 'category_id')->withTimestamps();
    }

    public function photos()
    {
        return $this->hasMany(GoodsPhoto::class, 'goods_id');
    }

    public function specValue()
    {
        return $this->belongsToMany(SpecsValue::class, config('ibrand.app.database.prefix', 'ibrand_') . 'base_goods_spec_relation', 'goods_id', 'spec_value_id')
            ->withPivot('spec_id', 'alias', 'img', 'sort')->withTimestamps();
    }

    public function getFactoryPriceAttribute()
    {
        return $this->attributes['factory_price'];
    }

    public function getCategories()
    {
        return $this->categories;
    }

    public function getKeyCode($type = null)
    {
        return $this->id;
    }

    public function getItemType()
    {
        return 'goods';
    }

    public function getChildKeyCodes()
    {
        $codes = [];
        $products = $this->products;
        foreach ($products as $product) {
            $codes[] = $product->getKeyCode();
        }

        return $codes;
    }

    public function getSeasonId()
    {
        return $this->season_id;
    }
}